from PySide6.QtWidgets import (QDialog, QVBoxLayout, QHBoxLayout, QLabel,
                               QPushButton, QTextEdit, QFrame)
from PySide6.QtCore import Qt
from PySide6.QtGui import QFont, QPixmap
import sys
import os


class AboutDialog(QDialog):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.theme = "dark"  # 默认主题
        self.initUI()

    def initUI(self):
        self.setWindowTitle("关于音频软件定义处理器")
        self.setModal(True)
        self.resize(500, 400)
        self.setMinimumSize(450, 350)

        layout = QVBoxLayout()
        layout.setSpacing(15)
        layout.setContentsMargins(20, 20, 20, 20)

        # 标题
        title_label = QLabel("音频软件定义处理器")
        title_font = QFont()
        title_font.setPointSize(16)
        title_font.setBold(True)
        title_label.setFont(title_font)
        title_label.setAlignment(Qt.AlignCenter)

        # 版本信息
        version_label = QLabel("版本 1.0")
        version_label.setAlignment(Qt.AlignCenter)

        # 分隔线
        separator = QFrame()
        separator.setFrameShape(QFrame.HLine)
        separator.setFrameShadow(QFrame.Sunken)

        # 程序描述
        description_text = """
        <p>音频软件定义处理器是一个基于 PySide6 和 Python 开发的音频处理应用程序，提供可视化的音频效果链设计功能。</p>

        <p><b>主要特性：</b></p>
        <ul>
          <li>可视化拖拽音频处理流程设计</li>
          <li>多种音频处理节点（增益、延迟、限幅器等）</li>
          <li>实时波形查看和分析</li>
          <li>完整的项目文件管理</li>
          <li>多路音频设备支持</li>
          <li>白天/黑夜主题切换</li>
        </ul>

        <p><b>工作组：</b></p>
        <ul>
          <li>郭宗搏</li>
        </ul>
        """

        description_edit = QTextEdit()
        description_edit.setHtml(description_text)
        description_edit.setReadOnly(True)
        description_edit.setFrameShape(QTextEdit.NoFrame)
        description_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAsNeeded)

        # 系统信息
        sys_info = f"""
        <p><b>系统信息：</b></p>
        <ul>
          <li>Python版本: {sys.version.split()[0]}</li>
          <li>操作系统: {sys.platform}</li>
          <li>工作目录: {os.getcwd()}</li>
        </ul>
        """

        sys_info_label = QLabel()
        sys_info_label.setText(sys_info)

        # 按钮
        button_layout = QHBoxLayout()
        close_button = QPushButton("关闭")
        close_button.clicked.connect(self.close)
        close_button.setFixedWidth(120)
        button_layout.addStretch()
        button_layout.addWidget(close_button)
        button_layout.addStretch()

        # 添加到布局
        layout.addWidget(title_label)
        layout.addWidget(version_label)
        layout.addWidget(separator)
        layout.addWidget(description_edit)
        layout.addWidget(sys_info_label)
        layout.addLayout(button_layout)

        self.setLayout(layout)

        # 应用主题
        self.applyTheme(self.theme)

    def setTheme(self, theme):
        """设置对话框主题"""
        self.theme = theme
        self.applyTheme(theme)

    def applyTheme(self, theme):
        """应用主题样式 - 修复字体颜色问题"""
        if theme == "light":
            self.setStyleSheet("""
                QDialog {
                    background-color: #f0f0f0;
                    color: #000000;
                    font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
                }
                QLabel {
                    color: #000000;
                    background-color: transparent;
                    font-size: 9pt;
                }
                QTextEdit {
                    background-color: #ffffff;
                    color: #000000;
                    border: 1px solid #c0c0c0;
                    border-radius: 5px;
                    padding: 10px;
                    font-size: 9pt;
                    line-height: 1.4;
                }
                QTextEdit QAbstractScrollArea {
                    color: #000000;
                }
                QTextEdit * {
                    color: #000000;
                }
                QPushButton {
                    background-color: #e0e0e0;
                    color: #000000;
                    border: 1px solid #c0c0c0;
                    border-radius: 3px;
                    padding: 8px 20px;
                    font-weight: bold;
                    font-size: 9pt;
                    min-width: 80px;
                }
                QPushButton:hover {
                    background-color: #d0d0d0;
                    border: 1px solid #a0a0a0;
                }
                QPushButton:pressed {
                    background-color: #c0c0c0;
                }
                QFrame {
                    color: #c0c0c0;
                }
            """)

            # 为特定标签设置更深的颜色
            title_label = self.findChild(QLabel, None)
            if title_label and "音频软件定义处理器" in title_label.text():
                title_label.setStyleSheet("color: #000000; font-size: 16pt; font-weight: bold;")

            version_labels = self.findChildren(QLabel)
            for label in version_labels:
                if "版本" in label.text():
                    label.setStyleSheet("color: #444444; font-size: 10pt;")
                    break

            # 强制设置QTextEdit中的HTML内容颜色
            description_edit = self.findChild(QTextEdit)
            if description_edit:
                description_edit.setStyleSheet("""
                    QTextEdit {
                        background-color: #ffffff;
                        color: #000000;
                        border: 1px solid #c0c0c0;
                        border-radius: 5px;
                        padding: 10px;
                        font-size: 9pt;
                        line-height: 1.4;
                    }
                    QTextEdit * {
                        color: #000000 !important;
                    }
                """)

            # 强制设置系统信息标签的颜色
            sys_info_labels = self.findChildren(QLabel)
            for label in sys_info_labels:
                if "系统信息" in label.text():
                    label.setStyleSheet("color: #000000; font-size: 9pt;")
                    break

        else:  # dark theme
            self.setStyleSheet("""
                QDialog {
                    background-color: #2b2b2b;
                    color: #ffffff;
                    font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
                }
                QLabel {
                    color: #ffffff;
                    background-color: transparent;
                    font-size: 9pt;
                }
                QTextEdit {
                    background-color: #353535;
                    color: #ffffff;
                    border: 1px solid #555555;
                    border-radius: 5px;
                    padding: 10px;
                    font-size: 9pt;
                    line-height: 1.4;
                }
                QTextEdit QAbstractScrollArea {
                    color: #ffffff;
                }
                QTextEdit * {
                    color: #ffffff;
                }
                QPushButton {
                    background-color: #404040;
                    color: #ffffff;
                    border: 1px solid #555555;
                    border-radius: 3px;
                    padding: 8px 20px;
                    font-weight: bold;
                    font-size: 9pt;
                    min-width: 80px;
                }
                QPushButton:hover {
                    background-color: #505050;
                    border: 1px solid #777777;
                }
                QPushButton:pressed {
                    background-color: #606060;
                }
                QFrame {
                    color: #555555;
                }
            """)

            # 为特定标签设置更亮的颜色
            title_label = self.findChild(QLabel, None)
            if title_label and "音频软件定义处理器" in title_label.text():
                title_label.setStyleSheet("color: #ffffff; font-size: 16pt; font-weight: bold;")

            version_labels = self.findChildren(QLabel)
            for label in version_labels:
                if "版本" in label.text():
                    label.setStyleSheet("color: #cccccc; font-size: 10pt;")
                    break

            # 强制设置QTextEdit中的HTML内容颜色
            description_edit = self.findChild(QTextEdit)
            if description_edit:
                description_edit.setStyleSheet("""
                    QTextEdit {
                        background-color: #353535;
                        color: #ffffff;
                        border: 1px solid #555555;
                        border-radius: 5px;
                        padding: 10px;
                        font-size: 9pt;
                        line-height: 1.4;
                    }
                    QTextEdit * {
                        color: #ffffff !important;
                    }
                """)

            # 强制设置系统信息标签的颜色
            sys_info_labels = self.findChildren(QLabel)
            for label in sys_info_labels:
                if "系统信息" in label.text():
                    label.setStyleSheet("color: #ffffff; font-size: 9pt;")
                    break